﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class BookInfoService : IBookInfoService
    {
        private IBookInfoDal _bookInfo;
        private IMapper _mapper;
        public BookInfoService(IBookInfoDal bookInfo, IMapper mapper)
        {
            _bookInfo = bookInfo;
            _mapper = mapper;
        }

        public async Task<int> AddBookInfoAsync(string bookName, string bookDescription, string publishingHouse, string bookPhoto, decimal bookPrice, int bookCateGoryId)
        {
            return await _bookInfo.AddAsync(new Bookinfo 
            {
                BooksName = bookName,
                BooksDescription = bookDescription,
                PublishingHouse = publishingHouse,
                BooksPhoto = bookPhoto,
                BooksPrice = bookPrice,
                BooksCategoryId = bookCateGoryId
            });
        }

        public async Task<int> DeleteBookInfoAsync(int id)
        {
            var data = await _bookInfo.QueryAsync(id);
            return data == null ? -1 : await _bookInfo.DeleteAsync(data);
        }

        public async Task<int> EditeBookInfoAsync(int id, string bookName, string bookDescription, string publishingHouse, string bookPhoto, decimal bookPrice, int bookCateGoryId)
        {
            var data = await _bookInfo.QueryAsync(id);
            if (data == null)
                return -1;
            data.BooksName = bookName;
            data.BooksDescription = bookDescription;
            data.PublishingHouse = publishingHouse;
            data.BooksPhoto = bookPhoto;
            data.BooksPrice = bookPrice;
            return await _bookInfo.EditAsync(data);
        }

        public async Task<List<BookInfoDto>> QueryBookInfoAllAsync()
        {
            var data = await _bookInfo.QueryAsync();
            return _mapper.Map<List<Bookinfo>,List<BookInfoDto>>(data);
        }

        public async Task<List<BookInfoDto>> QueryBookInfoByBookNameAsync(string bookName)
        {
            var data = await _bookInfo.QueryAsync(bif => bif.BooksName.Contains(bookName));
            return _mapper.Map<List<Bookinfo>, List<BookInfoDto>>(data);
        }

        public async Task<BookInfoDto> QueryBookInfoByIdAsync(int id)
        {
            var data = await _bookInfo.QueryAsync(id);
            return _mapper.Map<Bookinfo, BookInfoDto>(data);
        }
    }
}
